/*
 * PanelWaveInfo.java
 *
 * Created on January 2, 2008, 6:50 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import hextd.core.ContextNormal;
import hextd.wave.Wave;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author  hylke
 */
public class PanelWaveInfo extends javax.swing.JPanel {

	public static DecimalFormat form = new DecimalFormat("#,###.#");
	private ContextNormal context;
	//
	private Timer paintTimer;
	private TimerTask paintTask;
	private int paintTime = 0;

	/** Creates new form PanelWaveInfo */
	public PanelWaveInfo() {
	}

	public void setContext(ContextNormal c) {
		this.context = c;
		initComponents();
		this.panelEnemy_cur.setContext(c);
		this.panelEnemy_next.setContext(c);
	}

	@Override
	public void setVisible(boolean aFlag) {
		if (aFlag) {
			this.startPaintTimer();
		} else {
			this.stopPaintTimer();
		}
		super.setVisible(aFlag);
	}

	public void clearWaves() {
		this.panelEnemy_cur.clearEnemies();
		this.panelEnemy_next.clearEnemies();
		this.jLabel_cur_level.setText("-");
		this.jLabel_cur_health.setText("-hp");
		this.jLabel_cur_reward.setText("-€");
		this.jLabel_next_level.setText("-");
		this.jLabel_next_health.setText("-hp");
		this.jLabel_next_reward.setText("-€");
	}

	public void setWaveCur(Wave w) {
		this.jLabel_cur_level.setText("" + w.getLevel());
		this.jLabel_cur_health.setText("" + PanelWaveInfo.form.format(w.getBaseHealth()) + "hp");
		this.jLabel_cur_reward.setText("" + w.getBaseReward() + "€");
		synchronized (context.getGameTime()) {
			this.panelEnemy_cur.clearEnemies();
			for (Wave.EnemyInstance e : w.enemySet()) {
				this.panelEnemy_cur.addEnemy(e);
			}
			this.panelEnemy_cur.recalculateSize();
		}
		this.invalidate();
		this.panelEnemy_cur.invalidate();
		this.validate();
	}

	public void setWaveNext(Wave w) {
		this.jLabel_next_level.setText("" + w.getLevel());
		this.jLabel_next_health.setText("" + PanelWaveInfo.form.format(w.getBaseHealth()) + "hp");
		this.jLabel_next_reward.setText("" + w.getBaseReward() + "€");
		synchronized (context.getGameTime()) {
			this.panelEnemy_next.clearEnemies();
			for (Wave.EnemyInstance e : w.enemySet()) {
				this.panelEnemy_next.addEnemy(e);
			}
			this.panelEnemy_next.recalculateSize();
		}
		this.invalidate();
		this.panelEnemy_next.invalidate();
		this.validate();
	}

	public void doTick(int gameTime) {
		//synchronized (context.getGameTime()) {
			this.panelEnemy_cur.doTick(gameTime);
			this.panelEnemy_next.doTick(gameTime);
		//}
	}

	public synchronized void startPaintTimer() {
		if (this.paintTimer == null) {
			this.paintTimer = new Timer("PanelWaveInfoPaintTimer", true);
		}
		if (this.paintTask == null) {
			this.paintTask = new TimerTask() {

				@Override
				public void run() {
					doTick(paintTime++);
				}
			};
			this.paintTimer.schedule(paintTask, 50, 50);
		}
	}

	public synchronized void stopPaintTimer() {
		if (paintTask != null) {
			paintTask.cancel();
			paintTask = null;
		}
	}

	public void cleanUp() {
		if (paintTimer != null) {
			paintTimer.cancel();
			paintTimer = null;
		}
		paintTask = null;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel_cur_health = new javax.swing.JLabel();
        jLabel_cur_reward = new javax.swing.JLabel();
        panelEnemy_cur = new hextd.UI.PanelEnemy();
        jLabel_next_health = new javax.swing.JLabel();
        jLabel_next_reward = new javax.swing.JLabel();
        panelEnemy_next = new hextd.UI.PanelEnemy();
        jLabel_next_level = new javax.swing.JLabel();
        jLabel_cur_level = new javax.swing.JLabel();

        setBackground(Constants.colorBackGround);
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Current & Next Wave", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Bitstream Vera Sans"Constants.colorForeGround)); // NOI18N
        setForeground(Constants.colorForeGround);
        setLayout(new java.awt.GridBagLayout());

        jLabel_cur_health.setBackground(Constants.colorBackGround);
        jLabel_cur_health.setForeground(Constants.colorForeGround);
        jLabel_cur_health.setText("0hp");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        add(jLabel_cur_health, gridBagConstraints);

        jLabel_cur_reward.setBackground(Constants.colorBackGround);
        jLabel_cur_reward.setForeground(Constants.colorForeGround);
        jLabel_cur_reward.setText("0€");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        add(jLabel_cur_reward, gridBagConstraints);

        panelEnemy_cur.setBorder(null);
        panelEnemy_cur.setMinimumSize(new java.awt.Dimension(30, 30));
        panelEnemy_cur.setPreferredSize(new java.awt.Dimension(195, 30));
        panelEnemy_cur.setLayout(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(panelEnemy_cur, gridBagConstraints);

        jLabel_next_health.setBackground(Constants.colorBackGround);
        jLabel_next_health.setForeground(Constants.colorForeGround);
        jLabel_next_health.setText("0hp");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5);
        add(jLabel_next_health, gridBagConstraints);

        jLabel_next_reward.setBackground(Constants.colorBackGround);
        jLabel_next_reward.setForeground(Constants.colorForeGround);
        jLabel_next_reward.setText("0€");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        add(jLabel_next_reward, gridBagConstraints);

        panelEnemy_next.setBorder(null);
        panelEnemy_next.setMinimumSize(new java.awt.Dimension(30, 30));
        panelEnemy_next.setPreferredSize(new java.awt.Dimension(195, 30));
        panelEnemy_next.setLayout(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 0.01;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
        add(panelEnemy_next, gridBagConstraints);

        jLabel_next_level.setBackground(Constants.colorBackGround);
        jLabel_next_level.setForeground(Constants.colorForeGround);
        jLabel_next_level.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        add(jLabel_next_level, gridBagConstraints);

        jLabel_cur_level.setBackground(Constants.colorBackGround);
        jLabel_cur_level.setForeground(Constants.colorForeGround);
        jLabel_cur_level.setText("0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        add(jLabel_cur_level, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel_cur_health;
    private javax.swing.JLabel jLabel_cur_level;
    private javax.swing.JLabel jLabel_cur_reward;
    private javax.swing.JLabel jLabel_next_health;
    private javax.swing.JLabel jLabel_next_level;
    private javax.swing.JLabel jLabel_next_reward;
    private hextd.UI.PanelEnemy panelEnemy_cur;
    private hextd.UI.PanelEnemy panelEnemy_next;
    // End of variables declaration//GEN-END:variables
}
